﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;
using System.IO;

namespace GSLMS.WebAPP.Areas.Contract.Controllers.ConTmp
{
    
    public class ConTmpController : AdminBaseController
    {

        protected override string navTabId
        { get { return "Contract_ConTemp"; } }
       

        public ActionResult Index(FormCollection form)
        {
            var searchParma = new TB_LOAN_CONTRACTTMP();
           
            searchParma = base.SearchPara<TB_LOAN_CONTRACTTMP>(form, searchParma);

            searchParma.STATUS = string.IsNullOrEmpty(Request["Status"]) ? -1 : Helper.ToInt(Request["Status"],-1);
            searchParma.SYSTEMID = string.IsNullOrEmpty(Request["SYSTEMID"]) ? -1 : Helper.ToInt(Request["SYSTEMID"], -1);
            searchParma.TMPFLAG = string.IsNullOrEmpty(Request["TMPFLAG"]) ? -1 : Helper.ToInt(Request["TMPFLAG"], -1);
            searchParma.TMPTYPE = string.IsNullOrEmpty(Request["TMPTYPE"]) ? -1 : Helper.ToInt(Request["TMPTYPE"], -1);


            searchParma.StrSort = " id desc";

            ViewData["tmpFlagList"] = EnumHelper.ToSelectList<ContractEnum.ContractTmpFlag>();
            ViewData["systemIDList"] = EnumHelper.ToSelectList<ApplyEnum.RegSystemID>();
            ViewData["statusList"] = EnumHelper.ToSelectList<ContractEnum.IsEffective>();
            ViewData["tmpTypeList"] = EnumHelper.ToSelectList<ContractEnum.ContractTmpType>();

            var list = serviceContractTmp.GetList(searchParma);// ?? new List<TB_LOAN_CONTRACTTMP>();
            ViewData["RowCount"] = searchParma.RowCount;
            return View(list);
        }

        #region 新增

        [HttpGet]
        public ActionResult Create(int id)
        {
            
            ViewData["tmpFlag"] = id;
            ViewData["systemIDList"] = EnumHelper.ToSelectList<ApplyEnum.RegSystemID>();
            return View();
        }

        
        [HttpPost]
        public string Create(FormCollection collection)
        {
            string result = string.Empty;
           
            TB_LOAN_CONTRACTTMP model = new TB_LOAN_CONTRACTTMP();
            TryUpdateModel(model, collection);

            try
            {
                model.CREATEBY = CurentUser.UserName;
                model.CREATEDATE = DateTime.Now;

                #region 上传文件
                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {

                    UploadFileManage uploadFile = new UploadFileManage(Request.Files[0]);
                    uploadFile.ClassType = navTabId;
                    uploadFile.Save();
                    if (!uploadFile.Error)
                    {
                        //先删除旧文件
                        if (!string.IsNullOrEmpty(model.SAVEURL))
                        {
                            uploadFile.DeleteFile(model.SAVEURL);
                        }
                        model.SAVEURL = uploadFile.FileInfo["filepath"];
                    }
                    else
                    {
                        ajaxStr = string.Format(ajaxStr, 300, uploadFile.Message, navTabId, "", "");
                        return "{" + ajaxStr + "}";
                    }

                }
                else
                { 
                  //从服务器上创建模板文件
                    string path = "\\Upload" + "\\ContractDefaultFile\\tmp.doc";
                    string dirPath = "\\Upload" + "\\" + navTabId + "\\" + DateTime.Now.ToString("yyyyMMdd");
                    string newPath = "\\Upload" + "\\" + navTabId + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + Guid.NewGuid() + ".doc";
                    string dirAPath = HttpContext.Server.MapPath(dirPath.Replace("\\", "/"));
                    if (!Directory.Exists(dirAPath))
                    {
                        Directory.CreateDirectory(dirAPath);
                    }
                    
                    FileInfo fi = new FileInfo(HttpContext.Server.MapPath(path.Replace("\\", "/")));
                    fi.CopyTo(HttpContext.Server.MapPath(newPath.Replace("\\", "/")), true);
                    model.SAVEURL = newPath;
                }
                #endregion


                bool isSumbit = false;
                isSumbit = serviceContractTmp.Add(model); 
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                {
                    result = AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        private void InitSelectList()
        {
            //ViewData["SalesMan"] = GetListFromDict("SalesMan");
            //ViewData["RegStatus"] = EnumHelper.ToSelectList<ApplyEnum.RegStatus>();
            //ViewData["Source"] = EnumHelper.ToSelectList<ApplyEnum.RegSource>();
            //ViewData["Mediator"] = new SelectList(new List<TB_REST_CHANNEL>());
            //ViewData["BussType"] = EnumHelper.ToSelectList<ApplyEnum.RegBussType>();
            //ViewData["Product"] = GetListFromDict("Product");
            //ViewData["ImportantLevel"] = GetListFromDict("ImportantLevel");


            //ViewData["tmpFlagList"] = EnumHelper.ToSelectList<ContractEnum.ContractTmpFlag>();
            ViewData["systemIDList"] = EnumHelper.ToSelectList<ApplyEnum.RegSystemID>();
            //ViewData["statusList"] = EnumHelper.ToSelectList<ContractEnum.IsEffective>();
            //ViewData["tmpTypeList"] = EnumHelper.ToSelectList<ContractEnum.ContractTmpType>();


        }


        #endregion

        #region 编辑

                public ActionResult EditConTmp(long id)
                {
                    var model = serviceContractTmp.GetByID(id);
                    InitSelectList();
                    return View(model);
                }

                [HttpPost]
                public string EditConTmp(FormCollection form)
                {
                    string result = string.Empty;

                    TB_LOAN_CONTRACTTMP model = new TB_LOAN_CONTRACTTMP();
                    TryUpdateModel(model, form);

                    try
                    {
                        model.CREATEBY = CurentUser.UserName;
                        model.CREATEDATE = DateTime.Now;



                        bool isSumbit = false;
                        isSumbit = serviceContractTmp.Update(model);
                        if (isSumbit)
                        { result = AjaxResultSucess; }
                        else
                        {
                            result = AjaxResultFail;
                        }
                    }
                    catch (Exception ex)
                    {
                        base.ExceptionHand(ex);
                        result = AjaxResultFail;
                    }

                    return "{" + result + "}";
                }

        #endregion

        #region 查看

        public ActionResult VConTmp(long id)
        {
            var model = serviceContractTmp.GetByID(id);
            return View(model);
        }

        #endregion

        #region 删除

        public string DelConTmp(long id)
        {
            string result = string.Empty;
          
            try
            {
                var model = serviceContractTmp.GetByID(id);
                if (model.SAVEURL != null && model.SAVEURL.Trim() != "")
                {
                    FileInfo fi = new FileInfo(HttpContext.Server.MapPath(model.SAVEURL));
                    fi.Delete();
                }

                bool isSumbit = serviceContractTmp.Delete(id);
                if (isSumbit)
                { result = AjaxDeleteSucess; }
                else
                { result = AjaxDeleteFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxDeleteFail;
            }
            return "{" + result + "}";
        }

        #endregion

        #region 查看文档

        public ActionResult VConTmpFile(long id)
        {
            var model = serviceContractTmp.GetByID(id);
          

            return View(model);
        }

        #endregion

        #region 编辑文档

        public ActionResult ParamList(long id)
        {
            return View();
        }
        
        public ActionResult EditConTmpFile(long id)
        {


            var model = serviceContractTmp.GetByID(id);


            return View(model);
        }

        [HttpPost]
        public string EditConTmpFile(FormCollection form)
        {

            
            string result = string.Empty;
            //TB_APP_SPECIALINFO model = new TB_APP_SPECIALINFO();
            //TryUpdateModel(model, form);
            //try
            //{
            //    if (model.ID == 0)
            //    {
            //        model.CREATEBY = CurentUser.UserName;
            //        model.CREATEDATE = DateTime.Now;
            //    }
            //    else
            //    {
            //        model.SPAREFIELD2 = CurentUser.UserName;
            //        model.SPAREFIELD3 = DateTime.Now.ToString();
            //    }

            //    model.CUSTNAME = Request["HIDCUSTNAME"];

            //    bool isSumbit = model.ID > 0 ? serviceSpecailInfo.Update(model) : serviceSpecailInfo.Add(model);
            //    if (isSumbit)
            //    { result = AjaxResultSucess; }
            //    else
            //    { result = AjaxResultFail; }
            //}
            //catch (Exception ex)
            //{
            //    base.ExceptionHand(ex);
            //    result = AjaxResultFail;
            //}
            return "{" + result + "}";
        }

        #endregion
    }
}
